import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/media_asset_data_entity.dart';
import 'package:lcapp/data/zixun_data_entity.dart';
import 'package:lcapp/model/text/expandable_text.dart';
import 'package:lcapp/tools/date_util.dart';
import 'package:lcapp/widget/asr_cache_image.dart';
import 'package:lcapp/widget/lc_gesture_detector.dart';
import 'min_open_vip_dialog.dart';

class MinZixunItem extends StatefulWidget {
  final ZixunDataEntity entity;
  final Function clickFun;
  const MinZixunItem({Key? key, required this.entity, required this.clickFun}) : super(key: key);

  @override
  _MinZixunItemState createState() => _MinZixunItemState();
}

class _MinZixunItemState extends State<MinZixunItem> {

  @override
  Widget build(BuildContext context) {
    return lcGestureDetector(
      isLogin: true,
      onTap: (){
        if(widget.clickFun!=null){
          widget.clickFun();
          return;
        }
        if(widget.entity.vipFlag==1&&userInfo.vip!=1){
          SmartDialog.show(
              alignment: Alignment.bottomCenter,
              clickMaskDismiss: true,
              maskColor: Color.fromRGBO(0, 0, 0, 0.75), builder: (BuildContext context) {
                return  GestureDetector(
                  onTap: (){
                    SmartDialog.dismiss();
                  },
                  child: MinOpenVipDialogPage(),
                );
          },
          );
          return;
        }
        if(widget.entity.layout!=3){
          SmartDialog.show(
              alignment: Alignment.center,
              clickMaskDismiss: true,
              maskColor: Color.fromRGBO(0, 0, 0, 0.75), builder: (BuildContext context) {
                return zxWidget();
          },
          );
          return;
        }
        Navigator.of(context).pushNamed('ZixunDetailPage',arguments: widget.entity.id);
      },
      child: Container(
        color: Colors.white,
        margin: EdgeInsets.only(top: 1),
        padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 20.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              child: Row(
                children: [
                  Expanded(child: Container(
                    padding: EdgeInsets.only(right: 30.w),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          margin: EdgeInsets.only(bottom: 20.w),
                          child:RichText(
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                            text: TextSpan(
                              children: [
                                WidgetSpan(child: Visibility(
                                  child: Container(
                                    child:widget.entity.layout==3? Image.asset('assets/home/vip_wz.png',height: 30.w,):Image.asset('assets/home/vip_kx.png',height: 30.w,),
                                  ),
                                  visible: widget.entity.vipFlag==1,
                                )),
                                TextSpan(
                                  text:  widget.entity.title,
                                  style: TextStyle(
                                      color: Colors.black,
                                      fontWeight: FontWeight.w600,
                                      fontSize: 30.sp
                                  )
                                )
                              ]
                            ),
                          )
                          // Text(widget.entity.title,maxLines: 2,overflow: TextOverflow.ellipsis,style: TextStyle(
                          //     color: Colors.black,
                          //     fontWeight: FontWeight.w600,
                          //     fontSize: 30.sp
                          // ),),
                        ),
                        Visibility(child: Container(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(child:  Text(widget.entity.origin??'',maxLines: 1,style: TextStyle(
                                color:Colors.black,
                                fontSize: 24.sp,
                                fontWeight: FontWeight.w500,
                              ),)),
                              SizedBox(width: 20.w,),
                              Text('${widget.entity.createTime}',style: TextStyle(
                                  color:Colors.black,
                                  fontSize: 24.sp,
                                  fontWeight: FontWeight.w500
                              ),)
                            ],
                          ),
                        ),visible: widget.entity.layout==3,),
                        Visibility(child: Container(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text('${widget.entity.createTime}',style: TextStyle(
                                  color:Colors.black,
                                  fontSize: 24.sp,
                                  fontWeight: FontWeight.w500
                              ),)
                            ],
                          ),
                        ),visible: widget.entity.layout!=3,)
                      ],
                    ),
                  ),),
                  Visibility(child: ClipRRect(
                    borderRadius: BorderRadius.circular(10.w),
                    child: Container(
                      color: bgColor,
                      child: ASRCacheImage(
                        image: widget.entity.images!,
                        fit: BoxFit.cover,
                        width: 200.w,
                        height: 160.w,
                      ),
                    ),
                  ),visible: widget.entity.layout==3,)
                ],
              ),
            ),
            Visibility(child: Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    width: double.infinity,
                    child:  ExpandableText(
                      text:widget.entity.content??'',
                      style:TextStyle(
                          color:  widget.entity.flag == 1? Color(0xffd7431f):Color(0xff444444),
                          fontSize: 32.sp,
                          height: 1.6
                      ),
                      maxLines: 6,
                      textAlign: TextAlign.left,
                      btnOpenWidget: 80.w,
                      btnOpen: Container(
                        width: 80.w,
                        alignment: Alignment.center,
                        padding: EdgeInsets.symmetric(vertical: 6.w),
                        decoration: BoxDecoration(
                            color: Color(0xffFFCEB6),
                            borderRadius: BorderRadius.circular(6.w)
                        ),
                        child: Text('展开',style:TextStyle(
                            color:Color(0xffEA6B2B),
                            fontSize: 24.sp
                        )),),
                      // btnClose: Container(
                      //   padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 6.w),
                      //   decoration: BoxDecoration(
                      //       color: Color(0xffFFCEB6),
                      //       borderRadius: BorderRadius.circular(6.w)
                      //   ),
                      //   child: Text('收起',style:TextStyle(
                      //     color: Color(0xffEA6B2B),
                      //     fontSize: 24.sp
                      // )),),
                    ),
                  ),
                  SizedBox(height: 20.w,),
                  Visibility(child: GestureDetector(
                    onTap: (){
                      MediaAssetDataEntity entity = MediaAssetDataEntity();
                      entity.type=1;
                      entity.uri = widget.entity.images;
                      Navigator.of(context).pushNamed('MediaViewer', arguments: {
                        'assets': [entity],
                        'currentIndex': 0
                      });
                    },
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(10.w),
                      child: ASRCacheImage(
                        image: widget.entity.images!,
                        fit: BoxFit.cover,
                        width: 200.w,
                        height: 160.w,
                      ),
                    ),
                  ),visible: widget.entity.layout==2,)
                ],
              ),
            ),visible: widget.entity.layout!=3&&(widget.entity.vipFlag!=1||userInfo.vip==1)&&1==2,),
            // Container(
            //   height: 1,
            //   margin: EdgeInsets.symmetric(vertical: 20.w),
            //   color: Color(0xffE9E8E9),
            // ),
          ],
        ),
      ),
    );
  }

  Widget zxWidget(){
    if(widget.entity==null){
      return Container();
    }
    return Container(
      alignment: Alignment.center,
      color: Colors.transparent,
      margin: EdgeInsets.symmetric(horizontal: 30.w,vertical: 150.w),
      child: SingleChildScrollView(
        child: Container(
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10.w)
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Stack(
                children: [
                  ClipRRect(
                    child: ASRCacheImage(
                      width: 1.sw-60.w,
                      fit: BoxFit.fitWidth,
                      image: 'assets/home/zx_fx_lp_top.png',
                    ),
                    borderRadius: BorderRadius.circular(10.w),
                  ),
                  Positioned(child: GestureDetector(
                    onTap: (){
                      SmartDialog.dismiss();
                    },
                    child: Icon(Icons.close,color: Colors.white,size: 40.sp,),
                  ),left: 30.w,top: 30.w,)
                ],
              ),
              Container(
                margin: EdgeInsets.only(top: 30.w),
                padding: EdgeInsets.symmetric(horizontal: 60.w),
                child: Row(
                  children: [
                    Image.asset('assets/home/zx_hs_rl.png',width: 35.w,),
                    SizedBox(width: 20.w,),
                    Text('${widget.entity.createTime}',style: TextStyle(
                        color: Color(0xff444444),
                        fontSize: 28.sp,
                        fontWeight: FontWeight.w500
                    ),)
                  ],
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.symmetric(horizontal: 60.w,),
                margin: EdgeInsets.only(top: 30.w,bottom: 10.w),
                child: Text('${widget.entity.title}',style: TextStyle(
                    color: Colors.black,
                    fontSize: 36.sp,
                    fontWeight: FontWeight.w500
                ),),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.symmetric(horizontal: 60.w,),
                margin: EdgeInsets.only(bottom: 40.w),
                child: Container(
                  child: Text('${widget.entity.content??''}',style: TextStyle(
                      color: Color(0xff444444),
                      fontSize: 32.sp,
                      height: 1.5
                  ),),
                ),
              ),
              Visibility(child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.symmetric(horizontal: 60.w,),
                margin: EdgeInsets.only(bottom: 40.w),
                child: ASRCacheImage(
                  image: widget.entity.images!,
                  width: double.infinity,
                  fit: BoxFit.fitWidth,
                ),
              ),visible: widget.entity.images!=null&&widget.entity.images!.length>0,),
              Container(child: qrWidget(),padding: EdgeInsets.symmetric(horizontal: 30.w),)
            ],
          ),
        ),
      ),
    );
  }
  Widget qrWidget(){
    return Container(
      padding: EdgeInsets.only(left: 30.w,right: 30.w,bottom: 40.w),
      // height: 150.w,
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10.w),bottomRight: Radius.circular(10.w))
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text('下载粮策APP',style: TextStyle(
                  color: Color(0xffE45D11),
                  fontSize: 40.sp,
                  fontFamily: 'Schyler',
                  fontWeight: FontWeight.w500
              ),),
              SizedBox(height: 20.w,),
              Text('买卖存粮有策略，粮食行业都在用',style: TextStyle(
                  color: Color(0xff444444),
                  fontSize: 20.sp,
                  fontWeight: FontWeight.w500
              ),)
            ],
          ),
          Container(
            width: 0.5,
            height: 120.w,
            margin: EdgeInsets.only(left: 30.w,right: 45.w),
            color: Color(0xffE9E8E9),
          ),
          // QrImage(
          //   size: 140.w,
          //   padding: EdgeInsets.zero,
          //   data: '${BasVueurl}/#/invite?lcID=${userInfo.id}&shareId=${widget.entity.id}',
          // ),
        ],
      ),
    );
  }
  Widget timeWidget(){
    var now = new DateTime.now();
    return Container(
      margin: EdgeInsets.symmetric(vertical: 40.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(child: Container(
            height: 0.2,
            // width: 100.w,
            color: Color(0xff989898),
          )),
          SizedBox(width: 30.w,),
          Text('${now.year}-${DateUtil.unitFormat(now.month)}-${DateUtil.unitFormat(now.day)} ${DateUtil.unitFormat(now.hour)}:${DateUtil.unitFormat(now.minute)}:${DateUtil.unitFormat(now.second)}',style: TextStyle(
              color: Color(0xff989898),
              fontSize: 28.sp,
              fontWeight: FontWeight.w400
          )),
          SizedBox(width: 30.w,),
          Expanded(child: Container(
            height: 0.2,
            // width: 100.w,
            color: Color(0xff989898),
          )),
        ],
      ),
    );
  }

}
